package cn.ncupyh.comptest;

//数字每位数字的和
public class NumSum {
    public static void main(String[] args) {
        int num = 9;
        int sum = numSum1(num);
//        if (sum != -1) {
//            System.out.println(num + "的数字之和为:" + sum);
//        } else {
//            System.out.println("该数不是正数!");
//        }
        System.out.println(num + "的数字之和为:" + sum);

    }

    private static int numSum(int num) {
        if (num < 0) {
            return -1;
        } else {
            int sum = 0;
            while (num != 0) {
                sum += num % 10;
                num /= 10;
            }
            return sum;
        }
    }

    private static int numSum1(int num) {
        if (num <= 9) {
            return num;
        }
        int sum = num % 10 + numSum1(num / 10);
        return sum;
    }
}